package com.customer.crm.system.controller;

import com.customer.crm.system.pojo.DictPojo;
import com.customer.crm.system.pojo.Dict_Type_Pojo;
import com.customer.crm.system.service.DictIService;
import com.customer.crm.system.service.DictTypeIService;
import com.customer.crm.util.ResultData;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class List_DictType_Controller {
    @Resource
    private DictTypeIService dictTypeIService;

    @RequestMapping("/system/dict/DictAction_list.action")
    public String toDictList(){
        return "/views/system/dict/index_dict";
    }

    //加载字典数据
    @ResponseBody
    @RequestMapping("/DictTypeAction_findAll")
    public ModelMap dictTypeList(){
        List<Dict_Type_Pojo> list = dictTypeIService.list();
        ModelMap modelMap = new ModelMap();
        modelMap.addAttribute("data",list);
        return modelMap;
    }

    //找到添加字典类型页面
    @RequestMapping("/DictTypeAction_edit")
    public String toDictTypeEdit(){
        return "views/system/dict/insert_dictType";
    }
    //字典类型保存
    @ResponseBody
    @RequestMapping("/DictTypeAction_saveOrUpdate")
    public ModelMap dictTypeEdit(Dict_Type_Pojo dictTypePojo){
        ModelMap modelMap = new ModelMap();
        boolean b = dictTypeIService.save(dictTypePojo);
        modelMap.addAttribute("success",b);
        return modelMap;
    }

    //删除字典类型
    @ResponseBody
    @RequestMapping("/DictTypeAction_remove")
    public ModelMap removeDictType(@RequestParam("dt.id") String id){
        ModelMap modelMap = new ModelMap();
        boolean b = dictTypeIService.removeById(id);
        if (b==true){
            modelMap.addAttribute("success","删除成功");
            modelMap.addAttribute("message","删除成功");
        }else{
            modelMap.addAttribute("success","删除失败");
            modelMap.addAttribute("message","删除失败");
        }
        return modelMap;
    }

    //修改字典类型
    @RequestMapping("/toUpdateDictType")
    public String toUpdateDictType(@RequestParam("dt.id")String id,ModelMap modelMap){
        Dict_Type_Pojo byId = dictTypeIService.getById(id);
        modelMap.addAttribute("dictType",byId);
        return "/views/system/dict/update_dictType";
    }

    //保存字典类型修改
    @ResponseBody
    @RequestMapping("/updateDictType")
    public ModelMap updateDictType(Dict_Type_Pojo dictTypePojo){
        ModelMap modelMap = new ModelMap();
        boolean b = dictTypeIService.updateById(dictTypePojo);
        if (b==true){
            modelMap.addAttribute("success","修改成功");
        }else{
            modelMap.addAttribute("success","修改失败");
        }
        return modelMap;
    }
}
